using System;
using System.Collections.Generic;
using UnityEngine;

namespace FrameWork.TileMapManager
{
    [CreateAssetMenu(fileName = "MapModel", menuName = "Data/Map Model")]
    public class MapModel : ScriptableObject
    {
        private List<Vector3Int> cultivatedTiles;                    // 存放已经开垦的瓦片
        private Dictionary<Vector3Int, CropData> cropDatasDic;       // 存放种植作物的瓦片

        // 事件定义
        // 开垦
        public event Action<Vector3Int> OnTileCultivated;
        public event Action<Vector3Int, CropData> OnTileCrop;
        public event Action<Vector3Int> OnTileCropClear;
        
        public event Action<MapModel> OnUpdateInfo;
        
        // 只读属性
        public List<Vector3Int> CultivatedTiles => cultivatedTiles;
        public Dictionary<Vector3Int, CropData> CropDatasDicTiles => cropDatasDic;

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(List<Vector3Int> cultivatedTiles, Dictionary<Vector3Int, CropData> cropDatasDic)
        {
            this.cultivatedTiles = cultivatedTiles;
            this.cropDatasDic = cropDatasDic;
        }

        public void Initialize()
        {
            // 通知UI界面更新视图
            OnUpdateInfo?.Invoke(this);
        }

        #region Cultivate

        /// <summary>
        /// 耕地
        /// </summary>
        /// <param name="position"></param>
        public void CultivateTile(Vector3Int position)
        {
            if (IsTileCultivated(position))
            {
                Debug.Log($"瓦片 {position} 已经开垦");
                return;
            }

            // 添加到已开垦瓦片集合
            cultivatedTiles.Add(position);
            // 通知UI界面更新视图
            OnTileCultivated?.Invoke(position);
        }

        #endregion
        
        #region Crop

        // 种植
        public void PlantCropTile(Vector3Int position, string cropName)
        {
            if (IsTileCultivated(position))
            {
                if (cropDatasDic.ContainsKey(position))
                {
                    Debug.LogWarning("这里已经种植了作物" + position);
                    return;
                }
                cropDatasDic.Add(position, new CropData(CropManager.Instance.GetCropDic[cropName],position,1, cropName));
                // 视图更新
                OnTileCrop?.Invoke(position, cropDatasDic[position]);
            }
            else
            {
                Debug.LogWarning("这里没有开垦" + position);
            }
        }

        //
        public void WaterPlantCropTile(Vector3Int position)
        {
            if (!cropDatasDic.ContainsKey(position))
                return;
            cropDatasDic[position].WaterCrop();
            OnTileCrop?.Invoke(position, cropDatasDic[position]);
        }
        
        //
        public void HarvestCrop(Vector3Int position)
        {
            if(!cropDatasDic.ContainsKey(position))
                return;
            cropDatasDic[position].HarvestCrop();
            cropDatasDic.Remove(position);             // 移除该地区瓦片地图
            Debug.Log("收获");
            OnTileCropClear?.Invoke(position);
        }
        #endregion

        public bool IsTileCultivated(Vector3Int position)
        {
            return cultivatedTiles.Contains(position);
        }
    }
}